/*
 * @Author: your name
 * @Date: 2020-09-07 20:41:02
 * @LastEditTime: 2020-09-08 15:32:14
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /sort/sort.cpp
 */

#include <iostream>
using namespace std;

//冒泡排序1
void BubbleSort1(int *arry)
{
    int len=(sizeof(arry)/sizeof(arry[0]));  //定义数组长度
    for (int end =len-1;end >0;end--)     /* code */
    {
        for (int begin = 1; begin <=end;begin++)
        {
            if (arry[begin-1]>=arry[begin])
            {
                int temp=arry[begin];
                arry[begin]=arry[begin-1];
                arry[begin-1]=temp;
            }
        }
    }
}

//冒泡排序2
void BubbleSort2(int *arry)
{
    int len=(sizeof(arry)/sizeof(arry[0]));  //定义数组长度
    for (int i=0;i<len;i++)     /* code */
    {
        for (int j =i+1 ; j <len;j++)
        {
            if (arry[i]>arry[j])
            {
                int temp=arry[j];
                arry[j]=arry[i];
                arry[i]=temp;
            }
        }
    }
}

//优化1
void BubbleSort3(int *arry)
{
    int len=(sizeof(arry)/sizeof(arry[0]));  //定义数组长度
    for (int end =len-1;end >0;end--)     
    {
        bool sorted=true;
        for (int begin = 1; begin <=end;begin++)
        {
            if (arry[begin-1]>arry[begin])
            {
                int temp=arry[begin];
                arry[begin]=arry[begin-1];
                arry[begin-1]=temp;
                sorted=false;
            }
        }
        if(sorted)
            break;
    }
}

//优化2
void BubbleSort4(int *arry)
{
    int len=(sizeof(arry)/sizeof(arry[0]));  //定义数组长度
    for (int end =len-1;end >0;end--)     
    {
        int sortIndenx=0;
        for (int begin = 1; begin <=end;begin++)
        {
            if (arry[begin-1]>=arry[begin])
            {
                int temp=arry[begin];
                arry[begin]=arry[begin-1];
                arry[begin-1]=temp;
                sortIndenx=begin;
            }
        }
        end=sortIndenx;
    }
}
